﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InstrumentCheck.Module;
using InstrumentCheck.BLL;

namespace InstrumentCheck.WebAppUI
{
    public partial class CorrectMaintain : BasePage
    {
        Item currectItem = null;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["Form"] = null;
            Session["Routing"] = new FormRouting { FlowNO = 0 };
            if (!Page.IsPostBack)
            {
                ViewState["listItem"] = null;
                ViewState["currectItemIndex"] = null;
                ViewState["currectItem"] = null;
                ViewState["currectItemImageIndex"] = null;

                txtBuyDate.Attributes.Add("ReadOnly", "True");
                txtCreateDate.Attributes.Add("ReadOnly", "True");
                txtFirstCorrectDate.Attributes.Add("ReadOnly", "True");
                txtNeedCorrectDate.Attributes.Add("ReadOnly", "True");
                txtAssetNO.Attributes.Add("ReadOnly", "True");
                txtPRNO.Attributes.Add("ReadOnly", "True");

                InitialButton();
                InitialControls();
                SetControlsDisabled();
                List<Item> listItem = Maintain_bll.GetItemList(string.Empty);
                if (listItem == null)
                    return;
                ViewState["listItem"] = listItem;
                ViewState["currectItemIndex"] = 0;
                ViewState["currectItemImageIndex"] = 0;
                currectItem = listItem[0];
                ViewState["currectItem"] = currectItem;
                Session["AttachList"] = currectItem.ItemAttachment;
                ShowItem();
                ShowItemAttach();
                ShowItemImage();
                ShowForm();
                EnabledUpdateButton();
                EnaledMoveItemButton();
                EnaledMoveImageButton();
                
            }

        }

        private void InitialButton()
        {
            btnAdd.Enabled = (Session["UserInfo"] as User).IsToolingRoom;
            btnUpdate.Enabled = false;
            btnCancel.Enabled = false;
            btnSave.Enabled = false;
            btnPrevious.Enabled = false;
            btnNext.Enabled = false;
            btnQuery.Disabled = false;

            btnUploadImage.Enabled = false;
            btnDeleteImage.Enabled = false;
            btnPreviousImage.Enabled = false;
            btnNextImage.Enabled = false;
        }

        private void InitialControls()
        {
            ddlAssetType.SelectedIndex = -1;
            txtBuyDate.Text = string.Empty;
            txtAssetNO.Text = string.Empty;
            ddlBuyCurrency.SelectedIndex = -1;
            txtBuyAmount.Text = string.Empty;
            txtCreateDate.Text = System.DateTime.Today.ToString("yyyy/MM/dd");//string.Empty;
            txtFirstCorrectDate.Text = string.Empty;
            txtNeedCorrectDate.Text = string.Empty;
            ddlCorrectStatus.SelectedIndex = -1;
            lblNextCorrectDate.Text = string.Empty;
            ddlCorrectType.SelectedIndex = -1;
            ddlCurrentStatus.SelectedIndex = -1;
            ddlCorrectCurrency.SelectedIndex = -1;
            txtCorrectAmount.Text = string.Empty;
            txtPRNO.Text = string.Empty;

            txtStandard.Text = string.Empty;
            txtRemark.Text = string.Empty;

            rblCorrectCycle.SelectedIndex = -1;
            rblIsCorrect.SelectedIndex = -1;


            //lbl
            lblChiName.Text = string.Empty;
            lblSpec.Text = string.Empty;
            lblModuleNO.Text = string.Empty;
            lblBrand.Text = string.Empty;
            lblKeeper.Text = string.Empty;
            lblKeepDept.Text = string.Empty;

            lblCount.Text = string.Empty;

            lblCorrectAttach.Text = string.Empty;
            imageItem.ImageUrl = string.Empty;
            
            gvForm.DataSource = null;
            gvForm.DataBind();
        }

        private void SetControlsDisabled()
        {
            ddlAssetType.Enabled = false;
            ddlBuyCurrency.Enabled = false;
            ddlCorrectStatus.Enabled = false;
            ddlCorrectType.Enabled = false;
            ddlCurrentStatus.Enabled = false;
            ddlCorrectCurrency.Enabled = false;

            txtBuyDate.Enabled = false;
            txtCreateDate.Enabled = false;
            txtNeedCorrectDate.Enabled = false;
            txtFirstCorrectDate.Enabled = false;

            txtBuyAmount.ReadOnly = true;
            txtCorrectAmount.ReadOnly = true;
            txtStandard.ReadOnly = true;
            txtRemark.ReadOnly = true;

            imgBtnFindAssetNO.Disabled = true;
            imgBtnFindPRNO.Disabled = true;
            imgBtnUpload.Disabled = true;

            rblCorrectCycle.Enabled = false;
            rblIsCorrect.Enabled = false;
        }

        private void EnabledUpdateButton()
        {
            btnUpdate.Enabled = false;
            if ((Session["UserInfo"] as User).IsToolingRoom && ViewState["currectItem"] != null)
                btnUpdate.Enabled = true;
        }

        private void EnaledMoveItemButton()
        {
            btnPrevious.Enabled = false;
            btnNext.Enabled = false;
            if (ViewState["listItem"] == null)
                return;
            int currectItemIndex = Convert.ToInt32(ViewState["currectItemIndex"]);
            List<Item> listItem = (List<Item>)ViewState["listItem"];
            if (currectItemIndex > 0)
                btnPrevious.Enabled = true;
            if (currectItemIndex < listItem.Count - 1 && listItem.Count > 0)
                btnNext.Enabled = true;
        }

        private void EnaledMoveImageButton()
        {
            btnPreviousImage.Enabled = false;
            btnNextImage.Enabled = false;

            if (Session["AttachList"] == null)
                return;

            List<Attach> imageList = Session["AttachList"] as List<Attach>;
            imageList = (from i in imageList
                         where i.Type == AttachmentType.Picture
                         orderby i.ClientName
                         select i).ToList<Attach>();
            if (imageList == null)
                return;

            //string[] aryAttach = txtImageInfo.Text.Trim().Split(';');
            //if (aryAttach[0] == "")
            //    return;

            int currectItemImageIndex = Convert.ToInt32(ViewState["currectItemImageIndex"]);
            if (currectItemImageIndex > 0)
                btnPreviousImage.Enabled = true;
            if (currectItemImageIndex < imageList.Count - 1)
                btnNextImage.Enabled = true;
        }

        private void SetControEnabled()
        {
            //ddlAssetType.Enabled = true;
            ddlBuyCurrency.Enabled = true;
            ddlCorrectStatus.Enabled = true;
            ddlCorrectType.Enabled = true;
            ddlCurrentStatus.Enabled = true;
            ddlCorrectCurrency.Enabled = true;

            txtBuyDate.Enabled = true;
            //txtCreateDate.Enabled = true;
            txtNeedCorrectDate.Enabled = true;
            txtFirstCorrectDate.Enabled = true;

            txtBuyAmount.ReadOnly = false;
            txtCorrectAmount.ReadOnly = false;
            txtStandard.ReadOnly = false;
            txtRemark.ReadOnly = false;

            imgBtnFindAssetNO.Disabled = false;
            imgBtnFindPRNO.Disabled = false;
            imgBtnUpload.Disabled = false;
            

            rblCorrectCycle.Enabled = true;
            rblIsCorrect.Enabled = true;

            //btnAdd.Enabled = false;
            //btnUpdate.Enabled = false;
            //btnCancel.Enabled = false;
            //btnSave.Enabled = false;
            //btnPrevious.Enabled = false;
            //btnNext.Enabled = false;
            //btnQuery.Enabled = false;
            //btnUploadImage.Enabled = false;
            //btnDeleteImage.Enabled = false;
            //btnPreviousImage.Enabled = false;
            //btnNextImage.Enabled = false;

        }

        private void ShowItem()
        {
            if (ViewState["listItem"] == null)
                return;
            if (currectItem == null)
                return;
            
            ddlAssetType.SelectedValue = ((int)currectItem.AssetType).ToString();
            txtBuyDate.Text = currectItem.BuyDate.Value.ToString("yyyy/MM/dd");
            txtAssetNO.Text = currectItem.AssetNO;
            if (currectItem.CurrencyType.HasValue)
                ddlBuyCurrency.SelectedValue = currectItem.CurrencyType.ToString();
            if (currectItem.BuyAmount.HasValue)
                txtBuyAmount.Text = currectItem.BuyAmount.ToString();
            txtCreateDate.Text = currectItem.CreateDate.ToString("yyyy/MM/dd");
            txtFirstCorrectDate.Text = currectItem.FirstCorrectTime.Value.ToString("yyyy/MM/dd");
            txtNeedCorrectDate.Text = currectItem.NeedCorrectDate.Value.ToString("yyyy/MM/dd");
            if (currectItem.CorrectResult.HasValue)
                ddlCorrectStatus.SelectedValue = ((int)currectItem.CorrectResult).ToString();
            lblNextCorrectDate.Text = GetNextCorrectDate(currectItem.NeedCorrectDate.Value, currectItem.CorrectCycle.Value).ToString("yyyy/MM/dd");
            ddlCorrectType.SelectedValue = ((int)currectItem.CorrectType).ToString();
            ddlCurrentStatus.SelectedValue = ((int)currectItem.CurrentStatus).ToString();
            if (currectItem.CorrectCurrencyType.HasValue)
                ddlCorrectCurrency.SelectedValue = currectItem.CorrectCurrencyType.ToString();
            if (currectItem.CorrectAmount.HasValue)
                txtCorrectAmount.Text = currectItem.CorrectAmount.ToString();
            txtPRNO.Text = currectItem.PRNO;

            txtStandard.Text = currectItem.Standard;
            txtRemark.Text = currectItem.Remark;

            rblCorrectCycle.SelectedValue = ((int)currectItem.CorrectCycle).ToString();
            rblIsCorrect.SelectedIndex = currectItem.ProcessStatus > 0 ? 0 : 1;

            
            //lbl
            lblChiName.Text = currectItem.ChiName;
            lblSpec.Text = currectItem.Spec;
            lblModuleNO.Text = currectItem.ModelNO;
            lblBrand.Text = currectItem.Brand;
            lblKeeper.Text = currectItem.Keeper.EmpNO + "/" + currectItem.Keeper.EmpChineseName;
            lblKeepDept.Text = currectItem.Keeper.DeptNO;

            int currectItemIndex = Convert.ToInt32(ViewState["currectItemIndex"]);
            List<Item> listItem = (List<Item>)ViewState["listItem"];

            lblCount.Text = (currectItemIndex + 1).ToString() + "/" + listItem.Count.ToString();

            //lblCorrectAttach.Text = "";
            ViewState["AttachURL"] = "uploadAttach.aspx";
            ViewState["ImageURL"] = "uploadImage.aspx";
        }

        private void ShowItemAttach()
        {
            if (Session["AttachList"] == null)
                return;

            List<Attach> attachList = Session["AttachList"] as List<Attach>;
            attachList = (from i in attachList
                          where i.Type == AttachmentType.Attachment
                          orderby i.ClientName
                          select i).ToList<Attach>();

            //List<Attach> imageList1 = (List<Attach>)currectItem.ItemAttachment.Select(i => i.Type == AttachmentType.Attachment);
            //imageList1 = (List<Attach>)imageList1.OrderBy(i => i.ClientName).Select(i => i.Type == AttachmentType.Attachment);

            if (attachList == null)
                return;

            lblCorrectAttach.Text = string.Empty;
            //txtAttachInfo.Text = string.Empty;
            foreach (Attach attach in attachList)
            {
                lblCorrectAttach.Text += "<a href='" + this.AttachURL() + attach.SystemName + "' target='_blank'>" + attach.ClientName + "</a><br>";
                //txtAttachInfo.Text += attach.SystemName + "~" + attach.ClientName + ";";
            }
            if (lblCorrectAttach.Text != string.Empty)
                lblCorrectAttach.Text = lblCorrectAttach.Text.Substring(0, lblCorrectAttach.Text.Length - 4);
            //if (txtAttachInfo.Text != string.Empty)
            //    txtAttachInfo.Text = txtAttachInfo.Text.Substring(0, txtAttachInfo.Text.Length - 1);
            
        }

        private void ShowItemImage()
        {
            imageItem.ImageUrl = string.Empty;
            if (Session["AttachList"] == null)
                return;

            List<Attach> imageList = Session["AttachList"] as List<Attach>;
            imageList = (from i in imageList
                                      where i.Type == AttachmentType.Picture
                                      orderby i.ClientName
                                      select i).ToList<Attach>();

            //List<Attach> imageList1 = (List<Attach>)currectItem.ItemAttachment.Select(i => i.Type == AttachmentType.Picture);
            //imageList1 = (List<Attach>)imageList1.OrderBy(i => i.ClientName).Select(i => i.Type == AttachmentType.Picture);

            if (imageList == null)
                return;
            if (imageList.Count == 0)
                return;
            int currectItemImageIndex = Convert.ToInt32(ViewState["currectItemImageIndex"]);
            if (currectItemImageIndex < 0 || currectItemImageIndex >= imageList.Count)
                return;
            Attach imageAttach = imageList[currectItemImageIndex];
            imageItem.ImageUrl = this.AttachURL() + imageAttach.SystemName;
            
            //txtImageInfo.Text = string.Empty;
            //foreach (Attach image in imageList)
            //{
            //    txtImageInfo.Text += image.SystemName + "~" + image.ClientName + ";";
            //}
            //if (txtImageInfo.Text != string.Empty)
            //    txtImageInfo.Text = txtImageInfo.Text.Substring(0, txtImageInfo.Text.Length - 1);
        }

        private void ShowForm()
        {
            if (ViewState["listItem"] == null)
                return;
            if (currectItem == null)
                return;

            List<ApplyForm> formLit = Maintain_bll.GeAllFormListbyFasNO(currectItem.AssetNO);
            gvForm.DataSource = formLit;
            gvForm.DataBind();
        }

        protected void gvForm_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow)
                return;
            ApplyForm form = e.Row.DataItem as ApplyForm;

            Label lblFormNO = e.Row.FindControl("lblFormNO") as Label;
            //lblFormNO.Text = "<a href='#' onclick='javascript:var newwin=OpenForm(\"CorrectItem.aspx?FormNO=" + form.FormNO.ToString() 
            //    + "\",0,0,800,600,0,0,0,0,1)'>" + form.FormNO.ToString() + "</a>";
            lblFormNO.Text = "<a href=\"CorrectItem.aspx?ID=" + form.FormID.ToString() + "\" target=\"_blank\">" + form.FormNO.ToString("0000000000") +"</a>";
            e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
            //e.Row.Cells[1].Text = form.FormNO.ToString();
            e.Row.Cells[2].Text = form.Item.NeedCorrectDate.Value.ToString("yyyy/MM/dd");
            e.Row.Cells[3].Text = form.Item.CorrectCycle.ToString();
            e.Row.Cells[4].Text = GetNextCorrectDate(form.Item.NeedCorrectDate.Value, form.Item.CorrectCycle.Value).ToString("yyyy/MM/dd");
            e.Row.Cells[5].Text = form.FormStatus.ToString();
            e.Row.Cells[6].Text = form.ApplyReason;
         }

        private DateTime GetNextCorrectDate(DateTime CorrectDate, CorrectCycle cycle)
        {
            DateTime NextCorrectDate = CorrectDate;
            if ((int)cycle == 1)
                NextCorrectDate = NextCorrectDate.AddMonths(6);
            else if ((int)cycle == 2)
                NextCorrectDate = NextCorrectDate.AddYears(1);
            else
                NextCorrectDate = NextCorrectDate.AddYears(2);
            return NextCorrectDate;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            InitialControls();
            SetControEnabled();
            ddlAssetType.Enabled = true;
            btnAdd.Enabled = false;
            btnUpdate.Enabled = false;
            btnPrevious.Enabled = false;
            btnNext.Enabled = false;
            btnQuery.Disabled = true;
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
            btnUploadImage.Enabled = true;
            //btnDeleteImage.Enabled = true;
            //txtBuyDate.Enabled = true;
            //txtCreateDate.Enabled = true;
            //txtNeedCorrectDate.Enabled = true;
            //txtFirstCorrectDate.Enabled = true;
            ViewState["currectItem"] = null;
            ViewState["currectItemImageIndex"] = 0;
            Session["AttachList"] = new List<Attach>();
            EnabledDeleteImageButton();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            SetControEnabled();
            btnAdd.Enabled = false;
            btnUpdate.Enabled = false;
            btnPrevious.Enabled = false;
            btnNext.Enabled = false;
            btnQuery.Disabled = true;
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
            btnUploadImage.Enabled = true;
            //btnDeleteImage.Enabled = true;
            //txtBuyDate.Enabled = true;
            //txtCreateDate.Enabled = true;
            //txtNeedCorrectDate.Enabled = true;
            //txtFirstCorrectDate.Enabled = true;
            currectItem = ViewState["currectItem"] as Item;
            if (currectItem.ItemAttachment!= null)
                Session["AttachList"] = currectItem.ItemAttachment;
            else
                Session["AttachList"] = new List<Attach>();

            EnabledDeleteImageButton();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //.....
            string Mess = CheckItem();
            if (Mess != string.Empty)
            {
                this.AlertMessage(Mess);
                return;
            }
            List<Item> listItem = null;
            int currectItemIndex = 0;
            if (ViewState["currectItem"] == null)
            {
                currectItem = new Item();
                currectItem.Creater = Session["UserInfo"] as User;
                //currectItem.CreateDate = DateTime.Today;
                SavePageItem();
                Maintain_bll.SaveItem(currectItem);
                ViewState["currectItem"] = currectItem;
                
                if (ViewState["listItem"] == null)
                {
                    listItem = new List<Item>();
                    listItem.Add(currectItem);
                    ViewState["currectItemIndex"] = currectItemIndex;
                }
                else
                {
                    listItem = (List<Item>)ViewState["listItem"];
                    currectItemIndex = Convert.ToInt32(ViewState["currectItemIndex"]);
                    listItem.Insert(currectItemIndex, currectItem);
                }
                lblCount.Text = (currectItemIndex + 1).ToString() + "/" + listItem.Count;

            }
            else
            {
                currectItem = (Item)ViewState["currectItem"];
                string OldFasNO = currectItem.AssetNO;
                SavePageItem();
                Maintain_bll.UpdateItem(currectItem, OldFasNO);
                listItem = (List<Item>)ViewState["listItem"];
                currectItemIndex = Convert.ToInt32(ViewState["currectItemIndex"]);
                listItem.Insert(currectItemIndex, currectItem);
                listItem.RemoveAt(currectItemIndex + 1);
            }

            ViewState["currectItem"] = currectItem;
            ViewState["listItem"] = listItem;
            SetControlsDisabled();
            InitialButton();
            EnabledUpdateButton();
            EnaledMoveItemButton();
            EnaledMoveImageButton();
        }

        private string CheckItem()
        {
            string result = string.Empty;

            if (ddlAssetType.SelectedIndex <= 0)
            {
                result += "資產類別不能為空！";
                return result;
            }
            if (ddlBuyCurrency.SelectedIndex <= 0)
            {
                result += "購買幣別不能為空！";
                return result;
            }
            if (ddlCorrectStatus.SelectedIndex <= 0)
            {
                result += "校驗狀態不能為空！";
                return result;
            }
            if (ddlCorrectType.SelectedIndex <= 0)
            {
                result += "校驗類別不能為空！";
                return result;
            }
            if (ddlCurrentStatus.SelectedIndex <= 0)
            {
                result += "目前狀態不能為空！";
                return result;
            }
            if (ddlCorrectCurrency.SelectedIndex <= 0)
            {
                result += "校驗幣別不能為空！";
                return result;
            }

            if (txtBuyDate.Text == string.Empty)
            {
                result += "購買日期不能為空！";
                return result;
            }
            if (txtCreateDate.Text == string.Empty)
            {
                result += "創建日期不能為空！";
                return result;
            }
            if (txtNeedCorrectDate.Text == string.Empty)
            {
                result += "校驗日期不能為空！";
                return result;
            }
            if (txtFirstCorrectDate.Text == string.Empty)
            {
                result += "首校日期不能為空！";
                return result;
            }
            if (txtBuyAmount.Text == string.Empty)
            {
                result += "購買金額不能為空！";
                return result;
            }
            if (txtCorrectAmount.Text == string.Empty)
            {
                result += "費用不能為空！";
                return result;
            }
            if (txtAssetNO.Text == string.Empty)
            {
                result += "資產編號不能為空！";
                return result;
            }
            if (txtPRNO.Text == string.Empty)
            {
                result += "PR單號不能為空！";
                return result;
            }
            if (rblCorrectCycle.SelectedIndex == -1)
            {
                result += "校驗周期不能為空！";
                return result;
            }

            if ((ddlCurrentStatus.SelectedValue == "1") && (rblCorrectCycle.SelectedValue == "4" || ddlCorrectStatus.SelectedValue == "3" || ddlCorrectType.SelectedValue == "3"))
                result += "當為需校驗的時候，校驗週期、校驗結果和校驗區分不可以為不需填寫！";

            return result;
        }

        private void SavePageItem()
        {
            currectItem.AssetType = (AssetType)Convert.ToInt16(ddlAssetType.SelectedValue);
            currectItem.BuyDate = Convert.ToDateTime(txtBuyDate.Text);
            currectItem.AssetNO = txtAssetNO.Text;
            currectItem.CurrencyType = (CurrencyType)Enum.Parse(typeof(CurrencyType), ddlBuyCurrency.SelectedValue);
            currectItem.BuyAmount = Convert.ToDouble(txtBuyAmount.Text);
            currectItem.CreateDate = Convert.ToDateTime(txtCreateDate.Text);
            currectItem.FirstCorrectTime = Convert.ToDateTime(txtFirstCorrectDate.Text);
            currectItem.NeedCorrectDate = Convert.ToDateTime(txtNeedCorrectDate.Text);
            currectItem.CorrectResult = (CorrectResult)Convert.ToInt16(ddlCorrectStatus.SelectedValue);
            //lblNextCorrectDate.Text = GetNextCorrectDate(currectItem.NeedCorrectDate, currectItem.CorrectCycle).ToString("yyyy/MM/dd");
            currectItem.CorrectType = (CorrectType)Convert.ToInt16(ddlCorrectType.SelectedValue);
            currectItem.CurrentStatus = (AssetStatus)Convert.ToInt16(ddlCurrentStatus.SelectedValue);
            currectItem.CorrectCurrencyType = (CurrencyType)Enum.Parse(typeof(CurrencyType), ddlCorrectCurrency.SelectedValue);
            currectItem.CorrectAmount = Convert.ToDouble(txtCorrectAmount.Text);
            currectItem.PRNO = txtPRNO.Text;

            currectItem.Standard = txtStandard.Text;
            currectItem.Remark = txtRemark.Text;

            currectItem.CorrectCycle = (CorrectCycle)Convert.ToInt16(rblCorrectCycle.SelectedValue);
            currectItem.ModifyBy = Session["UserInfo"] as User;
            currectItem.ItemAttachment = Session["AttachList"] as List<Attach>;

            currectItem.ChiName = lblChiName.Text;
            currectItem.Spec = lblSpec.Text;
            currectItem.ModelNO = lblModuleNO.Text;
            currectItem.Brand = lblBrand.Text;
            currectItem.Keeper = new User();
            currectItem.Keeper.EmpNO = lblKeeper.Text.Split('/')[0];
            currectItem.Keeper.EmpChineseName = lblKeeper.Text.Split('/')[1];
            currectItem.Keeper.DeptNO = lblKeepDept.Text;

            //currectItem.ItemAttachment = new List<Attach>();
            //AddAttachment(currectItem, txtAttachInfo.Text.Trim().Split(';'), AttachmentType.Attachment);
            //AddAttachment(currectItem, txtImageInfo.Text.Trim().Split(';'), AttachmentType.Picture);
        }

        private void AddAttachment(Item item, string[] aryAttach, AttachmentType type)
        {
            foreach (string str in aryAttach)
            {
                string[] strAttach = str.Split('~');
                if (strAttach[0] == "")
                    break;
                Attach attach = new Attach();
                attach.FAS_NO = txtAssetNO.Text;
                attach.SystemName = strAttach[0];
                attach.ClientName = strAttach[1];
                attach.Type = type;
                item.ItemAttachment.Add(attach);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            InitialButton();
            InitialControls();
            SetControlsDisabled();
            List<Item> listItem = (List<Item>)ViewState["listItem"];
            if (listItem == null)
                return;
            int currectItemIndex = Convert.ToInt32(ViewState["currectItemIndex"]);
            currectItem = listItem[currectItemIndex];
            ViewState["currectItem"] = currectItem;
            ViewState["currectItemImageIndex"] = 0;
            Session["AttachList"] = currectItem.ItemAttachment;
            ShowItem();
            ShowItemAttach();
            ShowItemImage();
            ShowForm();
            EnabledUpdateButton();
            EnaledMoveItemButton();
            EnaledMoveImageButton();
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            //InitialButton();
            InitialControls();
            //SetControlsDisabled();
            List<Item> listItem = (List<Item>)ViewState["listItem"];
            int currectItemIndex = Convert.ToInt32(ViewState["currectItemIndex"]);
            currectItem = listItem[currectItemIndex - 1];
            ViewState["currectItem"] = currectItem;
            ViewState["currectItemIndex"] = currectItemIndex - 1;
            ViewState["currectItemImageIndex"] = 0;
            Session["AttachList"] = currectItem.ItemAttachment;
            ShowItem();
            ShowItemAttach();
            ShowItemImage();
            ShowForm();
            EnaledMoveItemButton();
            EnaledMoveImageButton();
            

            EnaledMoveItemButton();
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            //InitialButton();
            InitialControls();
            //SetControlsDisabled();
            List<Item> listItem = (List<Item>)ViewState["listItem"];
            int currectItemIndex = Convert.ToInt32(ViewState["currectItemIndex"]);
            currectItem = listItem[currectItemIndex + 1];
            ViewState["currectItem"] = currectItem;
            ViewState["currectItemIndex"] = currectItemIndex + 1;
            ViewState["currectItemImageIndex"] = 0;
            Session["AttachList"] = currectItem.ItemAttachment;
            ShowItem();
            ShowItemAttach();
            ShowItemImage();
            ShowForm();
            EnaledMoveItemButton();
            EnaledMoveImageButton();
            
            EnaledMoveItemButton();
        }

        protected void btnDeleteImage_Click(object sender, EventArgs e)
        {
            if (Session["AttachList"] == null)
                return;

            List<Attach> imageList = Session["AttachList"] as List<Attach>;
            imageList = (from i in imageList
                         where i.Type == AttachmentType.Picture
                         orderby i.ClientName
                         select i).ToList<Attach>();

            if (imageList == null)
                return;
            if (imageList.Count == 0)
                return;
            int currectItemImageIndex = Convert.ToInt32(ViewState["currectItemImageIndex"]);
            imageList.RemoveAt(currectItemImageIndex);
            if (currectItemImageIndex >= imageList.Count)
                currectItemImageIndex = currectItemImageIndex - 1;
            if (imageList.Count == 0)
                currectItemImageIndex = 0;
            ViewState["currectItemImageIndex"] = currectItemImageIndex;
            Session["AttachList"] = imageList;
            ShowItemImage();

            //string[] aryAttach = txtImageInfo.Text.Trim().Split(';');
            //if (aryAttach[0] == "")
            //    return;
            //int currectItemImageIndex = Convert.ToInt32(ViewState["currectItemImageIndex"]);
            ////if (currectItemImageIndex - 1 < 0 || currectItemImageIndex - 1 >= aryAttach.Length)
            ////    return;

            //txtImageInfo.Text = string.Empty;
            //for (int i = 0; i <= aryAttach.Length - 1; i++)
            //{
            //    if (i == currectItemImageIndex)
            //        continue;
            //    txtImageInfo.Text += aryAttach[i];
            //}
            //aryAttach = txtImageInfo.Text.Trim().Split(';');
            //if (currectItemImageIndex >= aryAttach.Length)
            //    currectItemImageIndex = currectItemImageIndex - 1;
            //string[] strAttach = aryAttach[currectItemImageIndex].Split('~');
            //if (strAttach[0] == "")
            //    return;
            //imageItem.ImageUrl = this.AttachURL() + strAttach[0];
            //ViewState["currectItemImageIndex"] = currectItemImageIndex;

            EnaledMoveImageButton();
            EnabledDeleteImageButton();
        }

        private void EnabledDeleteImageButton()
        {
            btnDeleteImage.Enabled = false;
            if (Session["AttachList"] == null)
                return;

            List<Attach> imageList = Session["AttachList"] as List<Attach>;
            imageList = (from i in imageList
                         where i.Type == AttachmentType.Picture
                         orderby i.ClientName
                         select i).ToList<Attach>();

            if (imageList == null)
                return;
            if (imageList.Count == 0)
                return;
            btnDeleteImage.Enabled = true;
        }

        protected void btnPreviousImage_Click(object sender, EventArgs e)
        {
            if (Session["AttachList"] == null)
                return;

            List<Attach> imageList = Session["AttachList"] as List<Attach>;
            imageList = (from i in imageList
                         where i.Type == AttachmentType.Picture
                         orderby i.ClientName
                         select i).ToList<Attach>();

            if (imageList == null)
                return;

            int currectItemImageIndex = Convert.ToInt32(ViewState["currectItemImageIndex"]);
            if (currectItemImageIndex - 1 < 0)
                return;
            Attach imageAttach = imageList[currectItemImageIndex - 1];
            imageItem.ImageUrl = this.AttachURL() + imageAttach.SystemName;
            ViewState["currectItemImageIndex"] = currectItemImageIndex - 1;

            //string[] aryAttach=txtImageInfo.Text.Trim().Split(';');
            //if (aryAttach[0] == "")
            //    return;
            //int currectItemImageIndex = Convert.ToInt32(ViewState["currectItemImageIndex"]);
            //if (currectItemImageIndex - 1 < 0)
            //    return;
            //string[] strAttach = aryAttach[currectItemImageIndex - 1].Split('~');
            //if (strAttach[0] == "")
            //    return;
            //imageItem.ImageUrl = this.AttachURL() + strAttach[0];
            //ViewState["currectItemImageIndex"] = currectItemImageIndex - 1;

            EnaledMoveImageButton();
        }

        protected void btnNextImage_Click(object sender, EventArgs e)
        {
            if (Session["AttachList"] == null)
                return;

            List<Attach> imageList = Session["AttachList"] as List<Attach>;
            imageList = (from i in imageList
                         where i.Type == AttachmentType.Picture
                         orderby i.ClientName
                         select i).ToList<Attach>();

            if (imageList == null)
                return;

            int currectItemImageIndex = Convert.ToInt32(ViewState["currectItemImageIndex"]);
            if (currectItemImageIndex + 1 >= imageList.Count)
                return;

            Attach imageAttach = imageList[currectItemImageIndex + 1];
            imageItem.ImageUrl = this.AttachURL() + imageAttach.SystemName;
            ViewState["currectItemImageIndex"] = currectItemImageIndex + 1;

            //string[] aryAttach = txtImageInfo.Text.Trim().Split(';');
            //if (aryAttach[0] == "")
            //    return;
            //int currectItemImageIndex = Convert.ToInt32(ViewState["currectItemImageIndex"]);
            //if (currectItemImageIndex + 1 >= aryAttach.Length)
            //    return;
            //string[] strAttach = aryAttach[currectItemImageIndex + 1].Split('~');
            //if (strAttach[0] == "")
            //    return;
            //imageItem.ImageUrl = this.AttachURL() + strAttach[0];
            //ViewState["currectItemImageIndex"] = currectItemImageIndex + 1;

            EnaledMoveImageButton();
        }

        protected void btnImage_Click(object sender, EventArgs e)
        {
            ViewState["currectItemImageIndex"] = 0;
            ShowItemImage();
            EnabledDeleteImageButton();
            EnaledMoveImageButton();
        }

        protected void btnAttach_Click(object sender, EventArgs e)
        {
            ShowItemAttach();
        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            ViewState["listItem"] = null;
            ViewState["currectItemIndex"] = 0;
            ViewState["currectItem"] = null;
            ViewState["currectItemImageIndex"] = 0;
            EnabledUpdateButton();
            EnaledMoveItemButton();
            EnaledMoveImageButton();
            //InitialButton();
            InitialControls();
            //SetControlsDisabled();
            List<Item> listItem = Maintain_bll.GetItemListByCondition(tAssetNO.Text, tChiName.Text, tKeepDept.Text);
            if (listItem == null)
                return;
            ViewState["listItem"] = listItem;
            currectItem = listItem[0];
            ViewState["currectItem"] = currectItem;
            Session["AttachList"] = currectItem.ItemAttachment;
            ShowItem();
            ShowItemAttach();
            ShowItemImage();
            ShowForm();
            EnabledUpdateButton();
            EnaledMoveItemButton();
            EnaledMoveImageButton();
        }

        protected void ddlCurrentStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCurrentStatus.SelectedValue == "" ||
                ddlCurrentStatus.SelectedValue == "1")
                return;

            ddlCorrectStatus.SelectedIndex = 4;
            ddlCorrectType.SelectedIndex = 3;
            rblCorrectCycle.SelectedIndex = 3;
        }

        
    }
}
